package com.xxg.renrenlogin.service;

import com.xxg.renrenlogin.domain.query.CodeVerifyQuery;
import com.xxg.renrenlogin.domain.result.BaseResult;
import com.xxg.renrenlogin.domain.result.LoginReturnCodeEnum;
import com.xxg.renrenlogin.exception.ErrorMessage;
import com.xxg.renrenlogin.exception.JiGuangException;
import com.xxg.renrenlogin.manager.jiguang.JiGuangManager;
import com.xxg.renrenlogin.manager.jiguang.query.JiGuangCheckCodeQuery;
import com.xxg.renrenlogin.manager.jiguang.query.JiGuangGetCodeQuery;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 极光认证服务实现
 *
 * @author xxg
 * @date 2020-09-25
 */
@Service
@AllArgsConstructor
public class JiGuangServiceImpl implements JiGuangService {

    private static final Logger logger = LoggerFactory.getLogger(JiGuangServiceImpl.class);
    private final JiGuangManager jiGuangManager;

    @Override
    public BaseResult getCode(String mobile) {
        JiGuangGetCodeQuery query = new JiGuangGetCodeQuery(mobile, "15582", "1");
        try {
            String msgId = jiGuangManager.getCode(query);
            return new BaseResult(LoginReturnCodeEnum.CODE_SUCCESS, "", msgId);
        } catch (JiGuangException e) {
            return new BaseResult(LoginReturnCodeEnum.CODE_FAILURE, e.getMessage(), null);
        }
    }

    @Override
    public boolean codeVerify(CodeVerifyQuery codeVerifyQuery) {
        JiGuangCheckCodeQuery query = new JiGuangCheckCodeQuery(codeVerifyQuery.getCode());
        boolean valid;
        try {
            valid = jiGuangManager.checkCode(codeVerifyQuery.getMsgId(), query);
        } catch (JiGuangException e) {
            return false;
        }
        return valid;
    }
}
